export const getUserAvatar = (username) => {
    return `http://123.207.205.119:8567/img_of_avatar/${username}.png?tempTime=${Date.now()}`
}

export const getFault = (faultId) => {
    return `http://123.207.205.119:8567/img_of_fault/${faultId}.png?tempTime=${Date.now()}`
}

export const fileToBase64 = (file) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onloadend = () => {
            resolve(reader.result);
        };
        reader.onerror = () => {
            reject(new Error("File reading error"));
        };
        reader.readAsDataURL(file);
    });
}

export const toBase64 = (file) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        let fileResult = '';
        reader.readAsDataURL(file.raw);
        reader.onload = () => fileResult = reader.result;
        reader.onerror = error => reject(error);
        reader.onloadend = () => resolve(fileResult);
    });
}
export const getImageFile = (url) => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.responseType = "blob";
        xhr.onload = () => {
            if (xhr.status === 200) {
                resolve(xhr.response);
            } else {
                reject(new Error(xhr.statusText));
            }
        };
        xhr.onerror = () => {
            reject(new Error("Network error"));
        };
        xhr.send();
    });
}

export const formatDate = (date) => {
    let time = new Date(date);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    let hour = time.getHours();
    let minute = time.getMinutes();
    let second = time.getSeconds();
    return `${year}/${month}/${day} ${hour}:${minute}:${second}`;
};